<script type="text/javascript">
$(function(){
	var pg = App.soagov.appSvcIndex.ui, pp = pg.params
		, hd = pg.children('.head'), bd = pg.children('.body')
		, gd = pg.seek('grid');
	
	gd.mac('grid', {
		cols : [{
			field: 'id', width: 32,
			title: '<input type="checkbox" name="all" />',
			render : function(r) {
				return '<input type="checkbox" name="keys" value="' + r.id + '"/>';
			}
		},{
			field: 'code', title : '服务编号', width: 240, sort: true, render: function(r){
				return $('<a href="#">' + r.code + '</a>').click(function(){
					spEdit(new Array(r.id),r.name+'已有协议',false);
				});
			}
		},{
			field: 'name', title : '服务名称', width: 240
		},{
			field: 'ver', title : '服务版本', width: 150
		},{
			field: 'opt', title : '操作', width: 70,
			render: function(r, tr, gd) {
				var p = $('<div></div>'), v = r.status,
					aa = ['enable', 'disable'], la = ['启用','禁用'],
					st = '<span class="a"></span>';
				if (App.logon.power('app.svc.remove')) {
					$(st).click(function(){
						mac.confirm('确定要 ' + la[v] + ' 队列 ' + r.code + ' 吗？', function(){
							$.post(App.soagov.ctx + '/svc/status.do', {
								id:r.id,
								status: r.status
							}, function(rs){
								var ro = mac.eval(rs);
								if (ro.success) {
									gd.load();
								}else{
									mac.alert(ro.data, ro.params);
								}
							});
						});
					}).append(la[v]).appendTo(p);
				}
				p.append('&nbsp;');
				if (App.logon.power('app.svc.remove')) {
					$(st).click(function(){
						if (!confirm('确定要从应用中移除' + r.code + '吗')) {
							return;
						}
						$.post(App.soagov.ctx + '/app/svc/remove.do', {
							id: pp.app.id,
							key: r.id
						}, function(rs){
							var ro = mac.eval(rs);
							if (ro.success) {
								gd.load();
							}else{
								mac.alert(ro.data, ro.params);
							}
						});
					}).append('移除').appendTo(p);
				}
				p.append('&nbsp;');
				return p;
			}
		},{
			field: 'memo', title : '备注', width: 240
		}],
		loader: {
			url: App.soagov.ctx + '/app/svc/list.do',
			params: {
				pageNo: 1, pageSize: 50,
				appId: pp.app.id
			},
			autoLoad: true
		},
		pagerLength: 10
	});
	
	pg.seek('all').click(function(){
		gd.seek('keys').prop('checked', $(this).prop('checked'));
	});
	
	//svc.save
	if(!App.logon.power('svc.save')){
		hd.children('button').hide();
	}
	hd.find('button').click(function(){
		var x = $(this).attr('name');
		if (x == 'remove') {
			/* alert("TODO:该功能暂未实现！"); */
			var s = gd.find('input[name=keys]:checked');
			s = $.map(s, function(c) {
				return c.value;
			});
			$.post(App.soagov.ctx + '/app/svc/remove.do', {
				id: pp.app.id,
				key: s.join(',')
			}, function(rs){
				var ro = mac.eval(rs);
				if (ro.success) {
					gd.seek('all').prop('checked', false);
					gd.load();
				}else{
					mac.alert(ro.data, ro.params);
				}
			});
		}
	});
	var spEdit = function(svcIds,title,flag){
		App.open({
				title:title,
				ctx: 'soagov',
				action: 'svcProtocolEdit',
				model: true,
				params: {svcIds:svcIds, appId:pp.app.id ,flag:flag}
		}, function(){
// 			gd.load();
		});
	}

	function adjust(){
		var h = pg.parent().height();
		bd.height(h - hd.height() - 32);
		gd.adjust();
	}
	$(window).resize(adjust);
	adjust();
});
</script>
<div class="head">
	<span>当前应用提供以下服务</span>
	<button name="remove" class="fr">移除所选的服务</button>
	<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="body">
	<div class="grid" name="grid"></div>
</div>